﻿/*
 * 
 * */

namespace AcctrueWMS.Foundation.Rule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Xml.Linq;
    using System.Reflection;
    using AcctrueWMS.Foundation.Web;
    using AcctrueWMS.Foundation.Data;

    public class ModelRuleManager : IModelRuleManager
    {
        public Collection<ModelRule> LoadRules(string path)
        {
            //try
            //{
            Collection<ModelRule> coll_rule = null;
            System.IO.DirectoryInfo dictinfo = new System.IO.DirectoryInfo(path);
            FileInfo[] files = dictinfo.GetFiles("*.xml");
            if (files.Length > 0)
            {
                coll_rule = new Collection<ModelRule>();

                foreach (FileInfo item in files)
                {
                    ModelRule modelRule = new ModelRule();

                    XDocument xd = XDocument.Load(item.FullName);
                    IEnumerable<XNode> rules = xd.Element("rules").Nodes();
                    foreach (XNode node in rules)
                    {
                        XElement rule = node as XElement;
                        modelRule.Name = String.IsNullOrEmpty(rule.Attribute("name").Value) ? String.Empty : rule.Attribute("name").Value;
                        modelRule.DisplayName = String.IsNullOrEmpty(rule.Attribute("displayName").Value) ? String.Empty : rule.Attribute("displayName").Value;
                        modelRule.Comment = String.IsNullOrEmpty(rule.Attribute("comment").Value) ? String.Empty : rule.Attribute("comment").Value;
                        modelRule.ModelName = String.IsNullOrEmpty(rule.Attribute("usefor").Value) ? String.Empty : rule.Attribute("usefor").Value;
                        modelRule.Status = String.IsNullOrEmpty(rule.Attribute("status").Value) ? String.Empty : rule.Attribute("status").Value;

                        if (!String.IsNullOrEmpty(rule.Attribute("type").Value))
                        {
                            modelRule.RuleType = (ModelRuleTypeEnum)Enum.Parse(typeof(ModelRuleTypeEnum), rule.Attribute("type").Value);
                        }

                        if (!String.IsNullOrEmpty(rule.Attribute("processor").Value))
                        {
                            string processor = rule.Attribute("processor").Value;
                            modelRule.Processor = Reflector.CreateInstance<IRuleEventProcessor>(processor.Split(',')[1], processor.Split(',')[0]);
                        }
                        coll_rule.Add(modelRule);
                    }
                    //rules.ToList().ForEach(rule =>
                    //    {
                    //        modelRule.Name = String.IsNullOrEmpty(rule.Attribute("name").Value) ? String.Empty : rule.Attribute("name").Value;
                    //        modelRule.DisplayName = String.IsNullOrEmpty(rule.Attribute("displayName").Value) ? String.Empty : rule.Attribute("displayName").Value;
                    //        modelRule.Comment = String.IsNullOrEmpty(rule.Attribute("comment").Value) ? String.Empty : rule.Attribute("comment").Value;
                    //        modelRule.ModelName = String.IsNullOrEmpty(rule.Attribute("usefor").Value) ? String.Empty : rule.Attribute("usefor").Value;
                    //        modelRule.Status = String.IsNullOrEmpty(rule.Attribute("status").Value) ? String.Empty : rule.Attribute("status").Value;

                    //        if (!String.IsNullOrEmpty(rule.Attribute("type").Value))
                    //        {
                    //            modelRule.RuleType = (ModelRuleTypeEnum)Enum.Parse(typeof(ModelRuleTypeEnum), rule.Attribute("type").Value);
                    //        }

                    //        if (!String.IsNullOrEmpty(rule.Attribute("processor").Value))
                    //        {
                    //            string processor = rule.Attribute("processor").Value;
                    //            modelRule.Processor = Reflector.GetAssembly<IRuleEventProcessor>(processor.Split(',')[1], processor.Split(',')[0]);
                    //        }
                    //        collModelRule.Add(modelRule);
                    //    });
                }
            }
            return coll_rule;
            //}
            //catch (Exception ex)
            //{
            //    throw new Exception("AcctrueWMS.Foundation.Rule.ModelRuleManager.LoadRules():\r\n", ex);
            //}
        }

        public void LogRule(Data.DataModel dataModel)
        {
            //
            //记录处理的数据模型
            //

            //using (TempDataContext context = new TempDataContext())
            //{
            //    //判断tb_LogRule中是否存在些规则，若不存在则创建
            //    tb_LogRule rule = context.tb_LogRule.First(lr => lr.modelname == dataModel.Name);
            //    if (rule == null)
            //    {
            //        rule = new tb_LogRule()
            //        {
            //            modelname = dataModel.Name,
            //            lrdate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
            //        };
            //        context.tb_LogRule.InsertOnSubmit(rule);
            //        context.SubmitChanges();
            //    }
            //    else
            //    {
            //        rule.lrdate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            //        context.SubmitChanges();
            //    }
            //}

        }
        #region LogError
        /*
        public void LogError(Data.DataModel dataModel)
        {
            //
            //记录没有处理成功的数据模型
            //
            using (TempDataContext context = new TempDataContext())
            {
                int lrid = -1;
                tb_LogRule rule = context.tb_LogRule.First(lr => lr.modelname == dataModel.Name);
                if (rule != null)
                    lrid = rule.lrid;

                if (lrid > 0)
                {
                    if (dataModel.Childs.Count > 0)
                    {
                        foreach (DataModel item in dataModel.Childs)
                        {
                            for (int i = 0; i < item.Rows.Count; i++)
                            {
                                tb_LogError error = new tb_LogError();
                                error.modelid = item.Rows[i]["odid"].Value.ToString();
                                error.letimes = "0";
                                error.flag = "";
                                error.lrid = lrid;
                                context.tb_LogError.InsertOnSubmit(error);
                            }
                        }
                        context.SubmitChanges();
                    }
                    else
                    {
                        tb_LogError error = new tb_LogError();
                        error.modelid = dataModel[0].Value.ToString();
                        error.letimes = "0";
                        error.flag = "";
                        error.lrid = lrid;
                        context.tb_LogError.InsertOnSubmit(error);
                        context.SubmitChanges();
                    }
                }

            }
        }
        */
        #endregion

        //public void LogError(Data.DataModel dataModel)
        //{
        //    //
        //    //记录没有处理成功的数据模型
        //    //
        //    using (TempDataContext context = new TempDataContext())
        //    {
        //        int lrid = -1;
        //        tb_LogRule rule = context.tb_LogRule.First(lr => lr.modelname == dataModel.Name);
        //        if (rule != null)
        //            lrid = rule.lrid;

        //        if (lrid > 0)
        //        {
        //            if (dataModel.Childs.Count > 0)
        //            {
        //                foreach (DataModel item in dataModel.Childs)
        //                {
        //                    for (int i = 0; i < item.Rows.Count; i++)
        //                    {
        //                        tb_LogError error = new tb_LogError();
        //                        error.modelid = item.Rows[i]["odid"].Value.ToString();
        //                        error.letimes = "0";
        //                        error.flag = "";
        //                        error.lrid = lrid;
        //                        context.tb_LogError.InsertOnSubmit(error);
        //                    }
        //                }
        //                context.SubmitChanges();
        //            }
        //            else
        //            {
        //                tb_LogError error = new tb_LogError();
        //                error.modelid = dataModel[0].Value.ToString();
        //                error.letimes = "0";
        //                error.flag = "";
        //                error.lrid = lrid;
        //                context.tb_LogError.InsertOnSubmit(error);
        //                context.SubmitChanges();
        //            }
        //        }

        //    }
        //}
    }
}
